--[[
	层级面板，专门做层级的
]]

local M = {
	gMain = nil,
	gContent = nil,
	gScroll = nil,
}

local rectFac = require("hier.rectFac")
local triangleFac = require("hier.triangle")

local topHeight = 40

local function resetY(g)
	if not coreDisplay.isGroup(g) then
		return
	end

	while (g ~= M.gContent) do
		local height = 0
		for i = 1, g.numChildren do
			g[i].y = height
			height = height + g[i].height
		end
		g = g.parent
	end
end

--[[
	parent:item`s parent
	gShow:the real display obj
	level：层级深度
	index：gShow在父组中的位置
]]
local function createItemFacker(parent, gShow, level, index)
	local gItem = display.newGroup()
	if parent then
		parent:insert(gItem)
	end

	gItem.proxyObj = gShow
	gItem.gRectItem = display.newGroup()
	gItem:insert(gItem.gRectItem)

	gItem.gChild = display.newGroup()
	gItem:insert(gItem.gChild)

	function gItem:expand()

	end

	function gItem:hide()

	end

	function gItem:updateSelf()
		local tOrigItems = {}
		local tProxyItems = {}

		for i = 1, gItem.gChild.numChildren do
			local obj = gItem.gChild[i].proxyObj
			tOrigItems[obj] = gItem.gChild[i]
		end


		for i = 1, gShow.numChildren do
			local obj = gShow[i]
			tProxyItems[obj] = obj
		end


		-- 删除不存在的物体，gChild proxy存在，但是在gShow中不存在
		for i = gItem.gChild.numChildren, 1, -1 do
			local obj = gItem.gChild[i].proxyObj
			if (not tProxyItems[obj]) then
				coreDisplay.cleanGroups(tOrigItems[obj])
			end
		end

		---- 显示新增的物体，gShow中存在，但是gChild中不存在
		for i = 1, gShow.numChildren do
			local obj = gShow[i]
			if (not tOrigItems[obj]) then
				local gItem1 = M.createItem(gItem.gChild, gShow[i], level + 1, i)
				gItem.gChild:insert(i, gItem1)
			end
		end
		gItem.isExpand = true
		resetY(gItem.gChild)
	end

	return gItem
end

function M.createItem(parent, gShow, level, index)
	local gItem = createItemFacker(parent, gShow, level, index)
	local bgRect = rectFac.createRect(gShow)
	gItem.gRectItem:insert(bgRect)

	local function createExpandIcon()
		local triangle = triangleFac.createTriangle()
		gItem.gRectItem:insert(triangle)
		triangle.x, triangle.y = 15 + (level - 1) * 20, 10

		inputUtils.touchDelegate({
			target = triangle,
			onRelease = function(event)
				local target = event.target
				if not gItem.isExpand then
					gItem:expand(event)
				else
					gItem:hide(event)
				end
			end
		})

		if not gShow.numChildren then
			triangle.isVisible = false
		end
		return triangle
	end
	local triangle = createExpandIcon()
	-- 展开
	function gItem:expand(event)
		if gItem.isExpand then
			return
		end

		gItem.isExpand = true
		transition.to(triangle, { rotation = 0, time = 10 })

		-- 创建子组
		gItem.gChild.y = gItem.gRectItem.height

		M.createItems(gItem.gChild, gShow, level + 1)
		resetY(gItem)
	end

	-- 关闭
	function gItem:hide(event)
		gItem.Expand = false
		transition.to(triangle, { rotation = -90, time = 10 })
		coreDisplay.cleanGroups(gItem.gChild, true)
		resetY(gItem)
	end

	local function getName()
		--local parent = gShow.parent
		--local name = ""
		--
		--if parent.name then
		--	name = parent.name .. "_" .. index
		--else
		--	name = "g"
		--end
		--
		--gShow.name = name

		local name = tostring(gItem.proxyObj)

		return name
	end

	local txt = display.newText({
		parent = gItem.gRectItem,
		text = getName(),
		x = 20 + level * 20, y = bgRect.height / 2,
		fontSize = 15,
		align = "left"
	})
	txt:setFillColor(unpack(colorCfg[7]))

	return gItem
end

function M.createItems(parent, gShow, level)
	for i = 1, gShow.numChildren do
		local gItem = M.createItem(parent, gShow[i], level, i)
	end
	resetY(parent)
end

local function drawScroll(params)
	local gScroll = widget.newScrollView({
		width = params.width, height = params.height,
		top = 7, left = 5,
		horizontalScrollDisabled = true,
		hideScrollBar = false,
		backgroundColor = colorCfg[8]
	})

	M.gContent = createItemFacker(gScroll, params.gShow, 1, 1)
	M.gContent.x = 5
	M.gContent.y = 2
	gPlatform.gContent.name = "g"
	M.createItems(M.gContent.gChild, params.gShow, 1)

	return gScroll
end

local function showChoosed(event)
	local tInfo = {}
	local target = event.target

	-- 查找自己所在位置
	while (target) ~= gPlatform.gContent do
		local index = 0
		local parent = target.parent

		for i = 1, parent.numChildren do
			if target == parent[i] then
				table.insert(tInfo, 1, i)
			else

			end
		end
		target = parent
	end

	local gList = M.gContent
	for i = 1, #tInfo do
		local index = tInfo[i]
		gList:expand()
		resetY(gList.gChild[index])
		gList = gList.gChild[index]
	end
	M.gScroll:insert(M.gContent)
end

local function onParentChange(event)
	local tInfo = {}
	local target = event.target

	-- 确定需要删除的obj位置
	while(target ~= gPlatform.gContent) do
		local parent = target.parent
		for i = 1, parent.numChildren do
			if target == parent[i] then
				table.insert(tInfo, 1, i)
			end
		end
		target = parent
	end

	-- 找到他的直属父组
	local gContent = M.gContent
	for i = 1, #tInfo do
		local index = tInfo[i]

		gContent = gContent.gChild[index]
	end

	gContent:updateSelf()
end

function M.new(params)
	M.gMain = dragBox.drawDragBox({
		width = 350,
		height = H - topHeight
	})
	M.gMain.x = 0
	M.gMain.y = topHeight

	local gHie = display.newGroup()
	M.gMain:insert(gHie)

	local gScroll = drawScroll({ width = 340, height = M.gMain.height - 20, gShow = gPlatform.gContent })
	gHie:insert(gScroll)
	M.gScroll = gScroll

	-- 显示舞台中点击的对象
	eventDispatcher:addEventListener(
		"chooseObj",
		showChoosed,
		function()
			return not coreDisplay.isDisplayObj(M.gMain)
		end
	)

	-- 物体从原有对象组删除
	eventDispatcher:addEventListener(
			"changeParentDelete",
			onParentChange,
			function()
				return not coreDisplay.isDisplayObj(M.gMain)
			end
	)

	-- 物体添加到新的对象组
	eventDispatcher:addEventListener(
			"changeParentAdd",
			onParentChange,
			function()
				return not coreDisplay.isDisplayObj(M.gMain)
			end
	)
	eventDispatcher:addEventListener(
			"platformAddObj",
			onParentChange,
			function()
				return not coreDisplay.isDisplayObj(M.gMain)
			end
	)

	return M.gMain
end

return M